package com.zx.frpc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zx.frpc.model.Common;
import com.zx.frpc.model.Http;
import com.zx.frpc.service.frpCommonService;
import com.zx.frpc.utils.ConfigurationFile;
import com.zx.frpc.utils.Result;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;

/**
 * @ClassName: frpServiceIpml
 * @Author: Zx
 * @Date: 2021/9/1 7:01 下午
 * @Version: v1.0
 */
@Service
public class frpCommonServiceIpml implements frpCommonService {


    @SneakyThrows
    @Override
    public Result get() {
        //读取frpc.ini文件
        //ConfigurationFile.readCfgValue()
        String path = System.getProperty("user.dir");
        path = path + "/frpc.ini";
        if(!new File(path).exists())return Result.build(201,"未找到frpc.ini文件");
        Common common = new Common();
        Field[] declaredFields = common.getClass().getDeclaredFields();
        String defaultValue = "未设置";
        for (Field declaredField : declaredFields) {
            String name = declaredField.getName();
            if("serialVersionUID".equals(name))continue;
            String value = ConfigurationFile.readCfgValue(path, "common", name, defaultValue);
            if(defaultValue.equals(value)) value = ConfigurationFile.readCfgValue(path, "ssh", name, defaultValue);
            declaredField.setAccessible(true);
            declaredField.set(common,value);
        }
        System.err.println(common);
        return Result.ok(common);
    }

    @Override
    public Result update(Common project)  {
        String path = System.getProperty("user.dir");
        path = path + "/frpc.ini";
        if(!new File(path).exists())return Result.build(201,"未找到frpc.ini文件");
        Field[] declaredFields = project.getClass().getDeclaredFields();
        try {
                for (Field declaredField : declaredFields) {
                    String name = declaredField.getName();
                    declaredField.setAccessible(true);
                    if("serialVersionUID".equals(name))continue;
                    if(ConfigurationFile.readCfgValue(path, "common", name, null)!=null){
                        ConfigurationFile.writeCfgValue(path, "common", name, declaredField.get(project).toString());
                    }else{
                        ConfigurationFile.writeCfgValue(path, "ssh", name, declaredField.get(project).toString());
                    }

                }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.build(201,e.getMessage());
        }
        return Result.ok();
    }

    @Override
    public Result getList() {
        String path = System.getProperty("user.dir");
        path = path + "/frpc.ini";
        ConfigurationFile configurationFile = new ConfigurationFile();
        ArrayList<Http> https = new ArrayList<>();
        try {
            configurationFile.IniReader(path);
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(configurationFile.getSections()));
            for (String section : jsonObject.keySet()) {
                if(!"common".equals(section) & !"ssh".equals(section)){
                    Http http = jsonObject.getObject(section, Http.class);
                    http.setSection(section);
                    System.err.println(http);
                    https.add(http);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }
}
